package com.huike.pmps.controller;

import com.huike.pmps.model.constants.GlobalConstants;
import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.entity.POCourse;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.POCourseService;
import com.huike.pmps.service.POCourseSupplierDetailService;
import com.huike.pmps.service.POService;
import com.huike.pmps.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@RestController
@RequestMapping("/api/po_courses")
@Api(tags = "PO课程管理")
@Slf4j
public class POCourseController {
    @Autowired
    private POService poService;
    @Autowired
    private POCourseService poCourseService;
    @Autowired
    private POCourseSupplierDetailService poCourseSupplierDetailService;


    @ApiOperation(value = "给PO添加课程", httpMethod = "POST")
    @PostMapping
    public Result insertOne(POCourse poCourse) {
        return poCourseService.insertOne(poCourse);
    }


    @ApiOperation(value = "根据ID更新课程数据", httpMethod = "PUT")
    @PutMapping
    public Result updateById(POCourse poCourse,HttpServletRequest request) {
        //从请求头中获取token
        Member member = (Member) request.getSession().getAttribute(TokenUtil.getToken(request));
        return poCourseService.updateById(poCourse,member);
    }

    @ApiOperation(value = "根据ID删除记录", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        return poCourseService.deleteById(id);
    }


    @ApiOperation(value = "根据ID更新记录状态", httpMethod = "PATCH")
    @ApiImplicitParam(name = "ids", value = "课程id集合", paramType = "query", dataType = "String")
    @PatchMapping
    public Result updatePoCourseByIds(String ids) {
        return poCourseService.updateStateById(ids);
    }


    @ApiOperation(value = "课程进度列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "po", value = "po单号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "customerName", value = "客户名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "regionName", value = "战区", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "provinceName", value = "省份名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "teamName", value = "客户成功名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "courseName", value = "课程名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "courseChargeName", value = "课程编导", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "courseState", value = "课程状态", paramType = "query", dataType = "String"),
    })
    @GetMapping
    public Result selectByCondition(HttpServletRequest request, PageInfo pageInfo, String po, String customerName, String regionName, String provinceName, String teamName,String courseName, String courseChargeName, String courseState) {
        //从请求头中获取token
        Member member = (Member) request.getSession().getAttribute(TokenUtil.getToken(request));
        return poCourseService.selectByCondition(member, pageInfo, po, customerName, regionName, provinceName,teamName, courseName, courseChargeName, courseState);
    }

    @ApiOperation(value = "根据主键id查询课程下的供应商", httpMethod = "GET")
    @GetMapping("/{id}/suppliers")
    public Result selectByPoId(@PathVariable("id") Integer id) {
        return poCourseSupplierDetailService.selectByPoCourseId(id);
    }


    /**
     * update by zcw 2021-07-06 根据ID获取课程状态状态
     * @param id 课程 id
     * @returncourseState 状态值
     */
    @ApiOperation(value = "根据ID获取课程状态", httpMethod = "GET")
    @GetMapping("/{id}")
    public Result selectPoCourseStateById(@PathVariable("id")Integer id) {
        return poCourseService.selectPoCourseStateById(id);
    }
}
